/*
 * @Author jiangzhiwei
 * @Date 2023-07-23 01:22:25
 * @LastEditors jiangzhiwei
 * @LastEditTime 2023-07-28 11:29:06
 * @FilePath \test-cron\src\components\yc-cron\components\options\common-designate-option\index.js
 * @Description 指定时间选项
 */
import { defineComponent } from 'vue-demi'
import { emitValue } from '../../../utils/componentutil'
import { LACK_FLAG, VALUE_NAME, VALUE_EMIT_NAME, PARSE_ERROR_EMIT_NAME } from '../../../utils/constant'
import { h } from '../../../utils/domutil'
import optionMixin from '../mixins/optionmixin'
import CommonLi from '../../base/common-li'
import CommonCheckboxGroup from '../../base/common-checkbox-group'
import CommonCheckbox from '../../base/common-checkbox'

const NAME = 'yc-cron-designate-option'
const regexp = /^(\d+)(,\d+)*$/

export default defineComponent({
  name: NAME,
  mixins: [optionMixin],
  props: {
    /** 时间总数。如1分钟内有60秒 */
    total: {
      type: Number,
      default: 60
    },
    /** 时间起始值 */
    startNum: {
      type: Number,
      default: 0
    },
    /** 值与label的映射 */
    valueMap: {
      type: Object,
      default: undefined
    }
  },
  data () {
    return {
      /** 指定时间列表 */
      list: []
    }
  },
  watch: {
    /** 根据表达式变化来设置选项内容 */
    expression: {
      handler (value) {
        if (regexp.test(value)) { // 表示匹配
          const list = value.split(',').map(item => Number.parseInt(item, 10))
          list.sort((a, b) => a - b)
          const max = this.total + this.startNum - 1
          if (list[0] < this.startNum) {
            emitValue(this, `最小值不能小于${this.startNum}`, PARSE_ERROR_EMIT_NAME)
          } else if (list[list.length - 1] > max) {
            emitValue(this, `最大值不能大于${max}`, PARSE_ERROR_EMIT_NAME)
          } else {
            this.list = list
            emitValue(this, this.label)
          }
        } else { // 不匹配
          if (value !== LACK_FLAG) {
            this.tryCancelCheck
          }
        }
      },
      immediate: true
    }
  },
  methods: {
    /** 更新 */
    onUpdate (value) {
      this.list = value
      emitValue(this, this.label)
      this.tryEmitExpression()
    },
    /** 获取表达式 */
    getExpression () {
      if (this.list.length) {
        this.list.sort((a, b) => a - b) // 排序
        return this.list.join(',')
      } else {
        return LACK_FLAG // 表示表达式缺失
      }
    }
  },
  render () {
    return h(
      CommonLi,
      {
        props: { [VALUE_NAME]: this[VALUE_NAME], label: this.label },
        on: { [VALUE_EMIT_NAME]: this.onInput },
        scopedSlots: {
          default: () => this.descriptions[0],
          content: () => {
            return h(
              CommonCheckboxGroup,
              {
                props: { [VALUE_NAME]: this.list },
                on: { [VALUE_EMIT_NAME]: this.onUpdate }
              },
              Array.apply(undefined, { length: this.total }).map((num, index) => {
                const label = index + this.startNum
                let text
                if (this.valueMap) {
                  text = this.valueMap[label]
                } else {
                  text = label < 10 ? `0${label}` : label
                }
                return h(
                  CommonCheckbox,
                  { props: { label }},
                  text
                )
              })
            )
          }
        }
      }
    )
  }
})